<?php
/**
 * 
 * API for Authorization server
 * 
 * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
 * @access public
 */
class CodeSpace_ServiceClient_Hal9000_Api extends CodeSpace_ServiceClient_Abstract
{
    /**
     * 
     * HTTP Request Holder
     * @var Zend_Rest_Client
     */
    private $_remoteServer = null;
    
    /**
     * 
     * Constructor
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param array $params
     */
    public function __construct ()
    {
        $this->_serviceIdentity = '90175dde-e37e-11e0-93fc-afa83b3142b8';
        $this->_remoteServer = new Zend_Rest_Client($this->getRequestUrl());
        parent::__construct();
    }

	/**
     * 
     * Return the all roles of the given client
     *
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $clientId
     * @return array|false Returns array data conatining roles; otherwise, false
     */
    public function getDefinedRolesOnClient ($clientId)
    {
        $this->setRequestParameter('service_type', self::REQUEST_READ);
        $this->setRequestParameter('client_id', $clientId);
        
        $result = $this->_remoteServer->restGet('/client/role', $this->getRequestParameters());
        
        if ($result->getStatus() == 200)
        {
            return json_decode($result->getBody());
        }
        else if ($result->getStatus() == 400 || $result->getStatus() == 401)
        {
            return false;
        }
    }
    
    /**
     * 
     * Checks the existance of the given role on the given client
     *
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $clientId
     * @param string $roleId
     * @return array|false Returns array data conatining roles; otherwise, false
     */
    public function clientHasRole($clientId, $roleId)
    {
        $this->setRequestParameter('service_type', self::REQUEST_READ);
        $this->setRequestParameter('role_id', $roleId);
        
        $result = $this->_remoteServer->restGet('/client/role/' . $clientId, $this->getRequestParameters());
        
        if ($result->getStatus() == 200)
        {
            return json_decode($result->getBody());
        }
        else if ($result->getStatus() == 400 || $result->getStatus() == 401)
        {
            return false;
        }
    }
    
	/**
     * 
     *
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $clientId
     * @return array|false Returns array data conatining roles; otherwise, false
     */
    public function defineRoleOnClient ($clientId, $roleId)
    {
        $this->setRequestParameter('service_type', self::REQUEST_CREATE);
        $this->setRequestParameter('role_id', $roleId);
        
        $result = $this->_remoteServer->restPost('/client/role/' . $clientId . '?_method=PUT', $this->getRequestParameters());
        
        var_dump($result); exit;
        if ($result->getStatus() == 200)
        {
            return json_decode($result->getBody());
        }
        else if ($result->getStatus() == 400 || $result->getStatus() == 401)
        {
            return false;
        }
    }
    
	/**
     * 
     * 
     *
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $clientId
     * @param string $roleId
     * @return array|false Returns array data conatining roles; otherwise, false
     */
    public function deleteRoleFromClient ($clientId, $roleId)
    {}

    /**
     * 
     * Return the all roles of the user who associated with the given access token
     *
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @return array|false Returns array data conatining roles; otherwise, false
     */
    public function getRolesOfUser ()
    {
        $this->setRequestParameter('service_type', self::REQUEST_READ);
        $result = $this->_remoteServer->restGet('/user/role', $this->getRequestParameters());
        
        if ($result->getStatus() == 200)
        {
            return json_decode($result->getBody());
        }
        else if ($result->getStatus() == 400 || $result->getStatus() == 401)
        {
            return false;
        }
    }

    /**
     * 
     * Check for a role key 
     *
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @return boolean Returns true, if the user has this role; otherwise, false
     */
    public function userHasRole ($roleKey)
    {
        $this->setRequestParameter('service_type', self::REQUEST_READ);
        $this->setRequestParameter('role_id', 'admin');
        
        $result = $this->_remoteServer->restGet('/user/role/' . $roleKey, $this->getRequestParameters());
        
        if ($result->getStatus() == 200)
        {
            return json_decode($result->getBody());
        }
        else if ($result->getStatus() == 400 || $result->getStatus() == 401)
        {
            return false;
        }
    }

    /**
     * 
     * Adds a role to the user associated with the given access token
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string Role Key
     * @return boolean Returns true, if the insert proccess is succed; otherwise, false
     */
    public function addRoleToUser ($role)
    {}

    /**
     * 
     * Deletes the given role key from user
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string Role Key
     * @return boolean Returns true, if the delete proccess is succed; otherwise, false
     */
    public function deleteRoleFromUser ($role)
    {}
    
	/**
     * 
     * 
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @return array Permission list
     */
    public function getUserPermissions ()
    {}
    
	/**
     * 
     * 
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $roleId
     * @return array Permission list
     */
    public function getRolePermissions ($roleId)
    {}
    
	/**
     * 
     * 
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $permissionId
     * @return boolean true, or false
     */
    public function addPermissionToUser ($permissionId)
    {}
    
	/**
     * 
     * 
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $permissionId
     * @return boolean true, or false
     */
    public function deletePermissionFromUser ($permissionId)
    {}
    
	/**
     * 
     * 
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $permissionId
     * @param string $roleId
     * @return boolean true, or false
     */
    public function addPermissionToRole ($permissionId, $roleId)
    {}
    
	/**
     * 
     * 
     * 
     * @author Tolga Akyüz <tolga@mikrobeta.com.tr>
     * @access public
     * @param string $permissionId
     * @param string $roleId
     * @return boolean true, or false
     */
    public function deletePermissionFromRole ($permissionId, $roleId)
    {}
}
?>